package com.npjscg.medicalpro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.common.Constants;

import com.npjscg.medicalpro.controller.dto.TemplateDTO;
import com.npjscg.medicalpro.entity.Check;
import com.npjscg.medicalpro.entity.Notification;
import com.npjscg.medicalpro.entity.Report;
import com.npjscg.medicalpro.exception.ServiceException;
import com.npjscg.medicalpro.mapper.CheckMapper;
import com.npjscg.medicalpro.mapper.NotificationMapper;
import com.npjscg.medicalpro.mapper.ReportMapper;
import com.npjscg.medicalpro.service.ICheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author raote
 * @since 2022-06-30
 */
@Service
public class CheckServiceImpl extends ServiceImpl<CheckMapper, Check> implements ICheckService {

    @Resource
    private CheckMapper checkMapper;

    @Resource
    private ReportMapper reportMapper;

    @Resource
    private NotificationMapper notificationMapper;

    @Autowired
    private Notification notification;

    @Autowired
    private  Check check;

    @Override
    public IPage<TemplateDTO> getCheckInfo(Page<TemplateDTO> page, int tid, int status, String temname) {
        if (temname == null)
            temname = "";
        QueryWrapper<TemplateDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        queryWrapper.like("temname", temname);
        return checkMapper.getUncheckedList(page, queryWrapper,tid);
    }

    @Override
    public void changeStatus(Integer rid, int currentStatus, int adminid) {

        Report report = reportMapper.selectById(rid);
        UpdateWrapper<Check> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("rid", rid);

        LocalDateTime dateTime = LocalDateTime.now();

        if (report == null)
            throw new ServiceException(Constants.CODE_600, "数据库中没有该记录");
        else{
            if (currentStatus == 1){
                //未通过
                updateWrapper.set("status",currentStatus);
                updateWrapper.set("msg","报表信息有误，未通过！");
                updateWrapper.set("adminid",adminid);
                updateWrapper.set("checkdate",dateTime);
                update(updateWrapper);
                //发送给用户通知
                notification.setId(null);
                notification.setUid(report.getUid());
                notification.setMsg("您的报表《"+report.getTemname()+"》未通过审核，请查看！");
                notification.setIsCheck(0);
                notificationMapper.insert(notification);

            } else if (currentStatus == 2) {
                //通过
                updateWrapper.set("status",currentStatus);
                updateWrapper.set("msg","上报成功，通过！");
                updateWrapper.set("adminid",adminid);
                updateWrapper.set("checkdate",dateTime);
                update(updateWrapper);
                //发给用户通过通知
                notification.setId(null);
                notification.setUid(report.getUid());
                notification.setMsg("您的报表《"+report.getTemname()+"》已通过审核！");
                notification.setIsCheck(0);
                notificationMapper.insert(notification);

            }
            else{
                throw new ServiceException(Constants.CODE_600, "不能设置成未审核，业务出错");
            }
        }
    }

    @Override
    public void changeMsg(Integer rid, String msg) {
        QueryWrapper<Check> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rid", rid);
        Check one = getOne(queryWrapper);
        if (one == null)
            throw new ServiceException(Constants.CODE_600 ,"数据库中没有此记录！");
        UpdateWrapper<Check> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("rid", rid);
        updateWrapper.set("msg", msg);
        update(updateWrapper);
    }

    public void addCheck(Report report){
        check.setId(null);
        check.setRid(report.getId());
        check.setMsg("用户已填写,待审核");
        check.setStatus(0);
        save(check);

    }

}
